These are my ideas about the grammar of ggplot2. ggplot2 is based on the grammar of graphics. But it has its own grammar too — it is a particular implementation of the grammar of graphics — and a lot of really helpful decisions are part of its grammar (like having set defaults for aesthetic scales, and scale and plot labels make it easier to build plots quickly). If you are teaching ggplot2, you might think about teaching the grammar of ggplot2 first and then come back to the grammar of graphics — because there will be more context for seeing how an implemenation relates to the larger philosophy. There are a lot of great resources out there already teaching the grammar of graphics which are sources of inspiration for the guide.

Not into human language grammar? Just ignor that first column!

What? How?
1. The Declarative Mood Declaring the data ggplot(data = gapminder) +
2. The Interogative Mood Asking for representation of variables by aesthetics (color, size, x position, etc.) (also known as aesthetic mapping) aes(color = pop) +
- Modifiers I modifying default aesthetic labels (and plot labels) labs(color = continent) +
labs(title = “my title”) +
- Modifiers II modifying the default coordinate system (how the positional aesthetics appear - x and y) coord_polar() +
- Modifiers III modifying default aesthetic scales scale_color_viridis_d() +
3. Nouns geometric layers taking on the aesthetics representing variables see below table geom_point() +
4.The Conditional Mood Making local, geom-specific declarations rather than global declarations geom_point(
data is
geom-specific
data = gapminder,
aesthetic representation
is geom-specific
aes(size = population),
aesthetics not representing variables; unmapped aesthetics (i.e. The Imperative Mood) color = “blue”
) +
5. Interjections Adding context with annotation layers annotate(geom = “point”,
x = 10,
y = 12,
color = “blue”)
6. Punctuation faceting breaks a plot into small plots (or “small multiples”) based on categorical variables facet_wrap(~continent)
7. Greetings themes changing plot look and feel theme_minimal()
8. The Written Language save plots with different resolutions and file formats ggsave(file = “plot.png”,
plot = g,
height = “4in”,
width = “6in”)
9. Composition Composing plots into ensembles library(patchwork)
(g1 + g2) | g3

library(cowplot)
plotgrid(…,)
10. Concision We’ve been intentionally verbose, but there are strategies for being concise last_plot(), switch out data with %+%, writing functions

A closer look at geoms.

Above, the geom topics are intentionally cursory. There are tons of geoms, and they can get distracting. But in the end, geoms are pretty fun - and there are a lot of them! Below gives you a preview of geometric marks used to describe continuous distributions, continuous-continuous joint distributions, visualizations of single series, of multiple series and discrete-discrete distributions.


A Closer Look at Color

A Closer Look at Text

External Theme Packages

Spatial with geom_sf and tmap



Network visualization {ggraph} and {tidygraph}


Animation with {gganimate}

Other Chapters

Data Wrangling Statistical Analysis